export class Content {
    /**
     * 内容
     * @param {'text' | 'image' | 'table' | 'video' | 'audio'} type 
     * @param {string} value 
     */
    constructor(type, value) {
        this.type = type
        this.value = value
    }
}

export class CmdOptionBase {
    /**
     * 操作的选项
     * @param {number} startIndex 起始字符序号
     * @param {number} endIndex 结束字符序号
     */
    constructor(startIndex, endIndex) {
        this.startIndex = startIndex
        this.endIndex = endIndex
    }
}

export class InsertOption extends CmdOptionBase {
    /**
     * 选项
     * @param {number} startIndex 起始字符序号
     * @param {number} endIndex 结束字符序号
     * @param {Content} content 内容
     */
    constructor(startIndex, endIndex, content) {
        super(startIndex, endIndex)
        this.content = content
    }
}

export class Operation {
    /**
     * 对于文档的操作
     * @param {'insert' | 'update' | 'delete'} cmd 
     * @param {CmdOptionBase} cmdOptionBase 
     */
    constructor(cmd, cmdOptionBase) {
        this.cmd = cmd
        this.cmdOptionBase = cmdOptionBase
    }
}

export class RichText {
    /**
     * 使用给定的html初始化富文本
     * @param {string} initHtml 
     */
    constructor(initHtml) {
        this.operations = this.parseHtmlToOperations(initHtml)
        this.tobeDeletedOperations = this.buildEmptyOperations()
    }

    /**
     * 转化html为操作数组
     * @param {string} html 
     * @returns {Operation[]}
     */
    parseHtmlToOperations(html = `<p><br></p>`) {
        let operations = []

        return operations
    }

    /**
     * 获取一个空的操作数组，仅仅用于类型提示
     * @returns {Operation[]}
     */
    buildEmptyOperations() {
        return []
    }

    /**
     * 获取一个空的字符串数组，仅仅用于类型提示
     * @returns {string[]}
     */
    buildEmptyStrings() {
        return []
    }

    /**
     * 
     * @param {Operation} operation 
     */
    addOpertion(operation) {
        this.operations.push(operation)
    }

    /**
     * 回退
     */
    back() {
        if (this.operations.length == 0) {
            return
        }
        let operation = this.operations.pop()
        this.tobeDeletedOperations.unshift(operation)
    }

    /**
     * 前进
     */
    forward() {
        if (this.tobeDeletedOperations.length == 0) {
            return
        }
        let operation = this.tobeDeletedOperations.shift()
        this.operations.push(operation)
    }

    renderToHtml() {
        let html = ''

        this.operations.forEach(operation => {
            switch (operation.cmd) {
                case 'insert':
                    break
                case 'update':
                    break
                case 'delete':
                    break
            }
        })

        return html
    }

    insert(operation) {

    }
}